package lumis.portal.metatag;

import java.util.List;

import lumis.portal.AccessDeniedException;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.acl.ChannelPermissions;
import lumis.portal.dao.DaoFactory;
import lumis.portal.manager.ManagerException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.acl.PagePermissions;
import lumis.util.HtmlUtil;
import lumis.util.ITransaction;
import lumis.util.PortalUtil;
import lumis.util.TextUtil;
import lumis.util.XmlUtil;

/**
 * Implementation for the Meta Tag Manager
 *
 * @version $Revision: 10642 $ $Date: 2009-07-13 18:04:25 -0300 (Mon, 13 Jul 2009) $
 * @since 4.0.11
 */
public class MetaTagManager implements IMetaTagManager
{

	public String add(SessionConfig sessionConfig, MetaTagConfig metaTagConfig, ITransaction transaction) throws PortalException
	{
		if(!XmlUtil.isValidXmlString(metaTagConfig.getContent()))
		{
			throw new ManagerException("STR_INVALID_XML;"+TextUtil.escapeLocalizationParameter(metaTagConfig.getContent()));
		}
		
		if (metaTagConfig.getType() == MetaTagConfig.METATAG_TYPE_CHANNEL)
		{
			if (!ManagerFactory.getChannelAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, ChannelPermissions.MANAGE_CHANNEL, transaction))
				throw new AccessDeniedException();
		}
		else
		{
			if (!ManagerFactory.getPageAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, PagePermissions.MANAGE_PAGE, transaction))
				throw new AccessDeniedException();				
		}
		
		if (metaTagConfig.getId() == null)
			metaTagConfig.setId(PortalUtil.generateNewGuid());
		
		DaoFactory.getMetaTagDao().add(metaTagConfig, transaction);

		return metaTagConfig.getId();
	}

	public void addOrUpdate(SessionConfig sessionConfig, MetaTagConfig metaTagConfig, ITransaction transaction) throws PortalException
	{
		if (metaTagConfig.getType() == MetaTagConfig.METATAG_TYPE_CHANNEL)
		{
			if (!ManagerFactory.getChannelAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, ChannelPermissions.MANAGE_CHANNEL, transaction))
				throw new AccessDeniedException();
		}
		else
		{
			if (!ManagerFactory.getPageAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, PagePermissions.MANAGE_PAGE, transaction))
				throw new AccessDeniedException();				
		}
		
		if(get(sessionConfig, metaTagConfig.getId(), transaction) == null)
			add(sessionConfig, metaTagConfig, transaction);
		else
			update(sessionConfig, metaTagConfig, transaction);
	}

	public void delete(SessionConfig sessionConfig, String id, ITransaction transaction) throws PortalException
	{
		MetaTagConfig metaTagConfig = DaoFactory.getMetaTagDao().get(id, transaction);
		
		if (metaTagConfig.getType() == MetaTagConfig.METATAG_TYPE_CHANNEL)
		{
			if (!ManagerFactory.getChannelAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, ChannelPermissions.MANAGE_CHANNEL, transaction))
				throw new AccessDeniedException();
		}
		else
		{
			if (!ManagerFactory.getPageAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, PagePermissions.MANAGE_PAGE, transaction))
				throw new AccessDeniedException();				
		}
		
		DaoFactory.getMetaTagDao().delete(id, transaction);
	}

	public MetaTagConfig get(SessionConfig sessionConfig, String id, ITransaction transaction) throws PortalException
	{
		return DaoFactory.getMetaTagDao().get(id, transaction);
	}

	public List<String> getIdsByParentId(SessionConfig sessionConfig, String parentId, ITransaction transaction) throws PortalException
	{
		return DaoFactory.getMetaTagDao().getIdsByParentId(parentId, transaction);
	}

	public void update(SessionConfig sessionConfig, MetaTagConfig metaTagConfig, ITransaction transaction) throws PortalException
	{
		if(!XmlUtil.isValidXmlString(metaTagConfig.getContent()))
		{
			throw new ManagerException("STR_INVALID_XML;"+TextUtil.escapeLocalizationParameter(metaTagConfig.getContent()));
		}
		
		if (metaTagConfig.getType() == MetaTagConfig.METATAG_TYPE_CHANNEL)
		{
			if (!ManagerFactory.getChannelAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, ChannelPermissions.MANAGE_CHANNEL, transaction))
				throw new AccessDeniedException();
		}
		else
		{
			if (!ManagerFactory.getPageAclManager().checkPermission(sessionConfig, metaTagConfig.parentId, PagePermissions.MANAGE_PAGE, transaction))
				throw new AccessDeniedException();				
		}
		
		DaoFactory.getMetaTagDao().update(metaTagConfig, transaction);
	}
}
